package com.ruoyi.project.module.ossconfig.service;

import java.util.Date;
import java.util.List;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.security.ShiroUtils;
import com.ruoyi.project.module.ossConfigLog.domain.OssConfigLog;
import com.ruoyi.project.module.ossConfigLog.service.IOssConfigLogService;
import com.ruoyi.project.module.ossconfig.domain.OSSConfig;
import com.ruoyi.project.module.task.mapper.TaskUtilsMapper;
import org.springframework.stereotype.Service;
import com.ruoyi.project.module.ossconfig.mapper.OSSConfigMapper;
import com.ruoyi.common.support.Convert;
import javax.annotation.Resource;

/**
 * 阿里云OSS资源 服务层实现
 * 
 * @author ruoyi
 * @date 2019-04-08
 */
@Service
public class OSSConfigServiceImpl implements IOSSConfigService
{
	@Resource
	TaskUtilsMapper taskUtilsMapper;

	@Resource
	private OSSConfigMapper ossConfigMapper;

	@Resource
	private IOssConfigLogService ossConfigLogService;

	/**
     * 查询阿里云OSS资源信息
     * 
     * @param ossConfigId 阿里云OSS资源ID
     * @return 阿里云OSS资源信息
     */
    @Override
	public OSSConfig selectConfigById(Integer ossConfigId)
	{
	    return ossConfigMapper.selectConfigById(ossConfigId);
	}
	
	/**
     * 查询阿里云OSS资源列表
     * 
     * @param OSSConfig 阿里云OSS资源信息
     * @return 阿里云OSS资源集合
     */
	@Override
	public List<OSSConfig> selectConfigList(OSSConfig OSSConfig)
	{
	    return ossConfigMapper.selectConfigList(OSSConfig);
	}
	
    /**
     * 新增阿里云OSS资源
     * 
     * @param ossConfig 阿里云OSS资源信息
     * @return 结果
     */
	@Override
	public int insertConfig(OSSConfig ossConfig)
	{
		Date currentDate = taskUtilsMapper.getCurrentDate();
		String userName = ShiroUtils.getUser().getLoginName();
		ossConfig.setCreateBy(userName);
		ossConfig.setCreateTime(currentDate);
		ossConfig.setUpdateBy(userName);
		ossConfig.setUpdateTime(currentDate);


		int i = ossConfigMapper.insertConfig(ossConfig);
		OssConfigLog ossConfigLog = new OssConfigLog();
		ossConfigLog.setOssConfigId(ossConfig.getOssConfigId());
		ossConfigLog.setFileContentStr(ossConfig.getFileContentStr());
		if(StringUtils.isNotEmpty(ossConfigLog.getFileContentStr())){
			ossConfigLogService.insertOssConfigLog(ossConfigLog);
		}
		return i;
	}
	
	/**
     * 修改阿里云OSS资源
     * 
     * @param ossConfig 阿里云OSS资源信息
     * @return 结果
     */
	@Override
	public int updateConfig(OSSConfig ossConfig)
	{
		Date currentDate = taskUtilsMapper.getCurrentDate();
		String userName = ShiroUtils.getUser().getLoginName();
		ossConfig.setCreateBy(null);
		ossConfig.setCreateTime(null);
		ossConfig.setUpdateBy(userName);
		ossConfig.setUpdateTime(currentDate);

		OssConfigLog ossConfigLog = new OssConfigLog();
		ossConfigLog.setOssConfigId(ossConfig.getOssConfigId());
		ossConfigLog.setFileContentStr(ossConfig.getFileContentStr());
		if(StringUtils.isNotEmpty(ossConfigLog.getFileContentStr())){
			ossConfigLogService.insertOssConfigLog(ossConfigLog);
		}
	    return ossConfigMapper.updateConfig(ossConfig);
	}

	/**
     * 删除阿里云OSS资源对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
	@Override
	public int deleteConfigByIds(String ids)
	{
		return ossConfigMapper.deleteConfigByIds(Convert.toStrArray(ids));
	}

	@Override
	public int copyModel(Long sourceId, OSSConfig ossConfig) {
		return insertConfig(ossConfig);
	}

}
